// package-star/pages/brand-distributor/index.js
import { PACKAGE_STAR_PAGE } from '../../../constants/route';
import { ManageTargetInfoUpdateMixins, ShareDistributorMixins, PageScrollListMixins } from '../../../behaviors/page';
import { navTo, navToAction, showLoading, hideLoading } from '../../../common/env';
import { setDistributorGuideFlag, getDistributorGuideFlag } from '../../../biztools/wex';
import { getDistributorList, updateDistributorAlias, getDistributorRequestList, getDistributeEndorsement } from '../../../api/brd';

Page({
    mixins: [ManageTargetInfoUpdateMixins, ShareDistributorMixins, PageScrollListMixins],
    data: {
        pageRouter: PACKAGE_STAR_PAGE,
        distributeGuideFlag: false,
        distributeRequestCount: 0,
        distributeInfo: {},
        shareInfo: {},
        distributorId: '',
        moreActionSheet: {
            show: false,
            list: [
                {
                    value: 'stats',
                    name: '活动数据',
                },
                {
                    value: 'remark',
                    name: '备注',
                },
            ],
        },
        remarkModal: {
            show: false,
        },
        loaded: false,
        inited: false,
    },
    onLoad(options) {
        let distributeGuideFlag = getDistributorGuideFlag();
        this.setData({ distributeGuideFlag });
    },
    onShow() {
        this._handleGetSetting();
        this._handleGetRequestCount();
    },
    async _handleGetSetting() {
        let { distributeGuideFlag } = this.data;
        const distributeInfo = await getDistributeEndorsement();
        !distributeGuideFlag && (distributeGuideFlag = distributeInfo.isOpen);
        const { name, brandId, homepageBg } = this.data.manageTargetInfo;
        const shareInfo = {
            name,
            brandId,
            homepageBg,
            title: distributeInfo.distributorTitle,
        };
        this.setData({
            shareInfo,
            distributeInfo,
            distributeGuideFlag,
        });
        this.setData({ inited: true });
    },
    async _handleGetRequestCount() {
        const { total } = await getDistributorRequestList({ pageSize: 1, pageNo: 1, isWaitAudit: 'true' });
        this.setData({ distributeRequestCount: total });
    },
    async _handleGetList(pageNo) {
        !this.data.loaded && showLoading('加载中...');
        const { pageData, filter, keyword } = this.data;
        let { ts } = pageData;
        pageNo === 1 && (ts = Date.now());
        const { rows = [], total } = await getDistributorList({ pageNo, ts, keyword, ...filter });
        this.setData({
            pageData: {
                ...pageData,
                refresh: false,
                ts,
                pageNo,
                total,
                rows: pageNo == 1 ? rows : [...pageData.rows, ...rows],
            },
        });
        hideLoading();
        if (!this.data.loaded) this.setData({ loaded: true });
    },
    handleUserMore(e) {
        this.setData({
            'distributorId': e.detail,
            'moreActionSheet.show': true,
        });
    },
    handleCloseMoreActionSheet() {
        this.setData({
            'moreActionSheet.show': false,
        });
    },
    handleSelectMoreActionSheet(e) {
        const _ = this;
        const { value } = e.detail;
        const { distributorId } = this.data;
        if (value === 'stats') {
            navTo(PACKAGE_STAR_PAGE['brandDistributorStats'], {
                distributorId
            });
        } else if (value === 'remark') {
            this.setData({
                'remarkModal.show': true,
            });
        }
        this.handleCloseMoreActionSheet();
    },
    async handleRemark(e) {
        const aliasName = e.detail;
        const { distributorId } = this.data;
        if (!aliasName) return;
        const flag = await updateDistributorAlias({ dtbId: distributorId, aliasName });
        if (flag) this._handleGetList(1);
    },
    handleNavTo(e) {
        const { page } = e.currentTarget.dataset;
        navTo(page);
    },
    handleNavToSetting() {
        const that = this;
        navToAction(PACKAGE_STAR_PAGE['brandDistributeSettingEndorse'], {
            fromStep: true,
            isOpen: true,
        }, {
            success(res) {
                setTimeout(() => {
                    that.setData({ distributeGuideFlag: true });
                }, 1000);
            },
        });
        setDistributorGuideFlag();
    },
})
